module Display {
	export class Collision extends egret.Sprite{
		private _hero:egret.Bitmap;
		private _txInfo:egret.TextField;
		private _dot:egret.Shape;

		private _status:number;
		private _type:number;

		public constructor() {
			super();
			this.addEventListener(egret.Event.ADDED_TO_STAGE,this.onAddedToStage,this);
		}

		/**
		 * 添加到舞台事件监听器
		 * @param evt:Event
		 */
		private onAddedToStage(evt:egret.Event):void{
			var imgLoader:egret.ImageLoader=new egret.ImageLoader;
			imgLoader.once(egret.Event.COMPLETE,this.imgLoaderCompleteHandler,this);
			imgLoader.load("resource/hero.png");
		}

		/**
		 * 图片加载完成监听器
		 * @param evt:Event
		 */
		private imgLoaderCompleteHandler(evt:egret.Event):void{
			//添加图片
			this._hero=new egret.Bitmap(evt.currentTarget.data);
			this.addChild(this._hero);
			this._hero.anchorOffsetX=this._hero.width/2;
			this._hero.anchorOffsetY=this._hero.height/2;
			this._hero.x=this.stage.stageWidth/2;
			this._hero.y=this.stage.stageHeight*0.6;
			//小圆点
			this._dot=new egret.Shape();
			this._dot.graphics.clear();
			this._dot.graphics.beginFill(0x00ff00);
			this._dot.graphics.drawCircle(0,0,5);
			//提示信息
			this._txInfo=new egret.TextField;
			this._txInfo.size=28;
			this._txInfo.x=150;
			this._txInfo.y=50;
			this._txInfo.textAlign=egret.HorizontalAlign.LEFT;
			this._txInfo.textColor=0x000000;
			this._txInfo.type=egret.TextFieldType.DYNAMIC;
			this._txInfo.multiline=true;
			this._txInfo.lineSpacing=6;
			this._txInfo.touchEnabled=true;
			this.addChild(this._txInfo);
			//文本触摸事件
			this._txInfo.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
				evt.stopImmediatePropagation();
				this._type=(this._type+1)%2;
				this._status=TouchCollideStatus.NO_TOUCH;
				this.updateInfo();
			},this);
			//舞台触摸事件
			this.stage.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.touchHandler,this);
			this.lauchCollisionTest();
		}

		/**
		 * 开启碰撞检测
		 */
		private lauchCollisionTest():void{
			this._type=CollideType.RECT;
			this._status=TouchCollideStatus.NO_TOUCH;
			this.updateInfo();
		}

		/**
		 * 更新文本
		 * 
		 */
		private updateInfo():void{
			this._txInfo.text=
			"碰撞检测结果："+(["放上手指！","想摸我？","别摸我！！！"][this._status])+
			"\n碰撞检测模式："+(["矩形","精确（非透明）"][this._type])+
			"\n轻触文字切换模式！";
		}

		/**
		 * 点击事件
		 * 开始-->移动-->结束
		 */
		private touchHandler(evt:egret.TouchEvent):void{
			switch(evt.type){
				case egret.TouchEvent.TOUCH_BEGIN:
					//确保点击位置不在文字区域
					if(!this._txInfo.hitTestPoint(evt.stageX,evt.stageY,false)){
						this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE,this.touchHandler,this);
						this.stage.once(egret.TouchEvent.TOUCH_END,this.touchHandler,this);
						this.addChild(this._dot);
						this.checkCollision(evt.stageX,evt.stageY);
					}
					break;
				case egret.TouchEvent.TOUCH_MOVE:
					this.checkCollision(evt.stageX,evt.stageY);
					break;
				case egret.TouchEvent.TOUCH_END:
					this.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE,this.touchHandler,this);
					if(this._dot.parent){
						this._dot.parent.removeChild(this._dot);
					}
					this._status=TouchCollideStatus.NO_TOUCH;
					this.updateInfo();
					break;
			}
		}

		/**
		 * 判断是否碰撞
		 */
		private checkCollision(x:number,y:number):void{
			var bResult:boolean=this._hero.hitTestPoint(x,y,this._type==CollideType.ACCURATE);
			if(bResult){
				this._status=TouchCollideStatus.TOUCH_COLLIDED;
			}else{
				this._status=TouchCollideStatus.TOUCH_NO_COLLIDED;
			}
			this.updateInfo();
			this._dot.x=x;
			this._dot.y=y;
		}
	}
	/**
	 * 碰撞状态
	 */
	class TouchCollideStatus{
		public static NO_TOUCH:number=0;
		public static TOUCH_NO_COLLIDED:number=1;
		public static TOUCH_COLLIDED:number=2;
	}
	/**
	 * 碰撞检测类型
	 */
	class CollideType{
		public static RECT:number=0;
		public static ACCURATE:number=1;
	}
}